package com.ucar.train.service.impl;

import com.ucar.train.dao.CrmEnterpriseMapper;
import com.ucar.train.dao.CrmMapper;
import com.ucar.train.dao.CrmPersonMapper;
import com.ucar.train.dao.StoreMapper;
import com.ucar.train.dto.CrmDTO;
import com.ucar.train.dto.CrmEnterpriseDTO;
import com.ucar.train.dto.CrmPersonDTO;
import com.ucar.train.dto.StoreDTO;
import com.ucar.train.service.CrmService;
import com.ucar.train.vo.CrmPersonVO;
import com.ucar.train.vo.CrmEnterpriseVO;
import com.ucar.train.vo.CrmVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wufuqiang
 * @title: CrmServiceImpl
 * @projectName:train-parent
 * @description: T0D0
 * @created 2019-08-14 16:28
 */
@Service
public class CrmServiceImpl implements CrmService {

    @Autowired
    CrmMapper crmMapper;

    @Autowired
    CrmPersonMapper crmPersonMapper;

    @Autowired
    CrmEnterpriseMapper crmEnterpriseMapper;

    @Autowired
    private StoreMapper storeMapper;
    /**
     * @Author: wufuqiang
     * @Description:用户登入
     * @Param:用户名
     * @return:用户信息(用户名，用户类型)
     * @Date: 2019/8/15
     */
    @Override
    public CrmDTO login(CrmDTO crmDTO) {
        return crmMapper.login(crmDTO);
    }

    /**
     * @Author: wufuqiang
     * @Description: 用户注册
     * @Param: crmDTO 用户信息
     * @return:注册结果
     * @Date: 2019/8/14
     */
    @Override
    public Integer personRegister(CrmDTO crmDTO, CrmPersonDTO crmPersonDTO) {
            //数据库中是否有这条记录，已username为准
            String flag = crmMapper.existByUsername(crmDTO.getUsername());
            //不存在
            if(flag == null){
                //插入数据，返回主键
                crmMapper.insert(crmDTO);
                //将主键作为子表的主键
                crmPersonDTO.setId(crmDTO.getId());
                crmPersonMapper.insert(crmPersonDTO);
                return 1;
            }
            return 0;
    }

    /**
     * @Author: wufuqiang
     * @Name: Register
     * @Description 企业用户注册
     * @Param crmTO 用户基本注册信息对象
     * @Param crmPersonDTO 用户特有信息对象
     * @return 注册结果 0：注册失败 1：注册成功
     * @Date: 2019/8/13
     */
    @Override
    public Integer enterpriseRegister(CrmDTO crmDTO, CrmEnterpriseDTO crmEnterpriseDTO) {
        //数据库中是否有这条记录，已username为准
        String flag = crmMapper.existByUsername(crmDTO.getUsername());
        if(flag == null){
            //插入数据，返回主键
            crmMapper.insert(crmDTO);
            crmEnterpriseDTO.setId(crmDTO.getId());
            crmEnterpriseMapper.insert(crmEnterpriseDTO);
            return 1;
        }
        return 0;
    }

    /**
     * @Author: wufuqiang
     * @Name: getCrm
     * @Description 查询用户信息
     * @Param crmDTO 用户身份信息
     * @return CrmVO 用户信息
     * @Date: 2019/8/13
     */
    @Override
    public CrmVO getCrm(CrmDTO crmDTO) {
        CrmDTO crm = crmMapper.selectById(crmDTO.getId());
        System.out.println("crm" + crm);

        if(crmDTO.getType() == 1 ){
            CrmPersonDTO crmPersonDTO = crmPersonMapper.selectById(crmDTO.getId());
            //CrmVO聚合,后期改在opr层,beanuitls
            CrmPersonVO crmPersonVO = new CrmPersonVO();
            crmPersonVO.setId(crmDTO.getId());
            crmPersonVO.setUsername(crm.getUsername());
            crmPersonVO.setPhone(crm.getPhone());
            crmPersonVO.setEmail(crm.getEmail());
            crmPersonVO.setName(crmPersonDTO.getName());
            crmPersonVO.setNumber(crmPersonDTO.getNumber());
            crmPersonVO.setLicense(crmPersonDTO.getLicense());
            return crmPersonVO;
        }else if(crmDTO.getType() == 2){
            CrmEnterpriseDTO crmEnterpriseDTO = crmEnterpriseMapper.selectById(crmDTO.getId());
            CrmEnterpriseVO crmEnterpriseVO = new CrmEnterpriseVO();
            crmEnterpriseVO.setId(crm.getId());
            crmEnterpriseVO.setUsername(crm.getUsername());
            crmEnterpriseVO.setPhone(crm.getPhone());
            crmEnterpriseVO.setEmail(crm.getEmail());
            crmEnterpriseVO.setName(crmEnterpriseDTO.getName());
            crmEnterpriseVO.setAddress(crmEnterpriseDTO.getAddress());
            crmEnterpriseVO.setIndustrNumber(crmEnterpriseDTO.getIndustrNumber());
            crmEnterpriseVO.setContact(crmEnterpriseDTO.getContact());
            crmEnterpriseVO.setContactNumber(crmEnterpriseDTO.getContactNumber());

            System.out.println(crmEnterpriseVO);
            return crmEnterpriseVO;
        }
        return null;
    }

    /**
     * @Author: wufuqiang
     * @Description:修改个人用户信息
     * @Param:a
     * @return:a
     * @Date: 2019/8/16
     */
    @Override
    public Integer updatePersonCrm(CrmDTO crmDTO,CrmPersonDTO crmPersonDTO) {

        crmMapper.update(crmDTO);
        crmPersonMapper.update(crmPersonDTO);
        return null;
    }

    /**
     * @Author: wufuqiang
     * @Description: 修改企业用户信息
     * @Param:a
     * @return:a
     * @Date: 2019/8/16
     */
    @Override
    public Integer updateEnterpriseCrm(CrmDTO crmDTO, CrmEnterpriseDTO crmEnterpriseDTO) {
        crmMapper.update(crmDTO);
        crmEnterpriseMapper.update(crmEnterpriseDTO);
        return 1;
    }

    @Override
    public List<StoreDTO> selectStore() {
        List<StoreDTO> storeDTOS = storeMapper.selectAllStore();
        System.out.println(storeDTOS);
        return storeDTOS;
    }

    @Override
    public CrmDTO selectCrmById(Long id) {
        return crmMapper.selectById(id);
    }

    @Override
    public CrmPersonDTO selectCrmPersonById(Long id) {
        return crmPersonMapper.selectById(id);
    }

    @Override
    public CrmEnterpriseDTO selectCrmEnterpriseById(Long id) {
        return crmEnterpriseMapper.selectById(id);
    }

    @Override
    public CrmDTO selectByCustomerID(Long crmID) {
        return crmMapper.selectByCustomerID(crmID);
    }

}
